<?php namespace App\Core\Metadata;

use App\Core\Exceptions\RuntimeError;

trait Namer {
    
    protected $nameKey = 'name';
    
    public function getNames($fields = '*'){
        $fieldKyes = $fields == '*' ? $fields : (is_array($fields) ? $fields : func_get_args());
        $data = [];
        foreach(array_keys($this->getFields($fieldKyes)) as $field){
            $data[$field] = $this->getName($field);
        }
        return $data;
    }
    
    public function getName($fieldName){
        $fieldItem = $this->getField($fieldName);
        if(!isset($fieldItem[$this->nameKey])){
            throw new RuntimeError('Matadata format error.Name key "'.$this->nameKey.'" not found in field "'.$fieldName.'".');
        }
        return $$fieldItem[$this->nameKey];
    }
}